A method to find the first 2n+1 integers having alternating bit sum equal to n
------------------------------------------------------------------------------

    There are four simple rules to find the first 2n+1 integers i, in ascending
order, having alternating bit sum S(i) equal to a given n . The first rule is,

I - The least integer i_1 such that S(i_1) = n is (4^n-1)/3 , Cf. A002450.

    The number of bits of i_1 is 2n-1 because the binary representation of i_1
has n bits one, and n-1 bits zero. Only for n=2, i_1 is prime. All the others
values of n gives i_1 composed.

    In what follows "even bits" mean the bits of a number corresponding to the
even powers of two. The other bits will be the "odd bits".

    If we turn-off one bit of i_1, we obtain x, and S(x) = S(i_1)-1, so we have
to add an even bit b to x to compensate. The bit b goes to the left of the MSB
of x because all the even bits of x are ones, except the bit turned-off. To find
numbers in ascending order we turn-off the MSB of i_1. So we have the rule,

II - The second least number i_2 such that S(i_2) = n is given by,
i_2 = i_1 - 2^(2n-2) + 2^(2n).

    Because i_1 has 2n-1 bits, the MSB of i_1 corresponds to 2^(2n-2). The even
bit added corresponds to 2^(2n). For example, if n=5,

i_1 = 101010101, so i_2 = 10 || 001010101 = 10001010101.

    Note that if we try to find i_2 turning-on an odd bit of i_1, we must add
the referenced bit b to compensate, and the number so obtained would be greater
than i_2.

    The rule II can be implemented by the PARI function,

II()={
i = (4^n - 1)/3 - 2^(2*n-2) + 2^(2*n);
if(isprime(i),
 an++ )
};

    The variables i, n, and an are global. The if statement was added since in
A184907 we count the primes in the first 2n+1 values of i.

    We obtain the next n-1 values, i_3, ... , i_(n+1) by the rule,

III - turn-on the last bit b turned-off, and turn-off the next bit to the right
of b. Stop when the "bit 2" is turned on. Because b corresponds successively to
powers 2n-2, 2n-4, ... , 2, the number of bits turned-on is n-1.

    We begin turning-on the bit 2n-2 because it is the unique even bit of a_2
equal to zero. To compensate we turn-off the next even bit, which will become
the unique even bit equal to zero, etc.

    The rule III can be implemented by the PARI function,

III()={
w = 2^(2*n-2);
for(j=1, n-1,
 i += w;
 w /= 4;
 i -= w;
 if(isprime(i),
  an++ )
 )
};

    For example, for n=5, we have the next n-1 values of i depicted below

Powers of two  876543210
       i_2 = 10001010101
               v v
             10100010101
                 v v
             10101000101
                   v v
             10101010001
                     v v
             10101010100

    We obtain the next n values i_(n+2), ... , i_(2n+1) by the rule,

IV - turn-on both the LSB of i_(n+1), and the next bit b to the left of it.
Proceed turning-off the last bit b turned-on, and turning-on the next bit to the
left of b. Stop when the bit 2n-1 is turned on. Because b corresponds
successively to powers 1, 3, ... , 2n-1, the number of b's turned-on is n.

    The rule IV can be implemented by the PARI function,

IV()={
i+=3;
if(isprime(i),
 an++ );
w=2;
for(j=1, n-1,
 i -= w;
 w *= 4;
 i += w;
 if(isprime(i),
  an++ )
 )
};

    For example, when n = 5, we have i_7 to a_11 depicted below.
Powers of two 9876543210
       i_6 = 10101010100
                      vv
             10101010111
                    v v
             10101011101
                  v v
             10101110101
                v v
             10111010101
              v v
             11101010101
